home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / spoc88 / tbcoml / param.bas < prev    next >
BASIC Source File  |  1988-05-08  |  4KB  |  126 lines

  1. '
  2. ' Author: Duke Kamstra
  3. ' Mod. Date: 5/8/88
  4. '
  5. '
  6. ' To use these routines in your own program, keep them in an
  7. ' include file. When you need to manage command line parameters
  8. ' in a program include these routines by inserting the
  9. ' metastatement:
  10. '    $INCLUDE "PARAM"
  11. ' in your program. Be sure to set the named constant
  12. ' %MAXPARAMETERS appropriately for your application. If the
  13. ' number of parameters given on the command line is larger
  14. ' than %MAXPARAMETERS the extras are ignored.
  15.  
  16.  
  17. %MAXPARAMETERS = 25 ' Maximum # of parameters that can be read by the
  18.             ' program. Should never be larger than 64 since
  19.             ' DOS only allows a 127 character command line.
  20.  
  21. DIM Parameters$(0:%MAXPARAMETERS)    ' String array used to store
  22.                     ' parameters
  23.  
  24. %TRUE = 1    ' Named constant representing boolean value
  25.  
  26. DEF FNParamCount%
  27. ' Return the number of command line parameters passed to the program.
  28. ' Store each of the parameters in the SHARED string array
  29. ' Parameters$(). Note the function will only process up to
  30. ' %MAXPARAMETERS command line parameters.
  31. '
  32. ' The first time the function is called is processes the parameter
  33. ' list and sets a flag Initialized% to indicate that the command
  34. ' line doesn't need to be processed again. Any subsequent calls to
  35. ' the function will return the value stored in Result%.
  36.  
  37.   STATIC Initialized%    ' Flag indicating parameters have been read
  38.               ' and data structure has been initialized.
  39.   STATIC Result%    ' Store result after calling the function
  40.               ' the first time
  41.   SHARED Parameters$()    ' Global variable to store parameter data
  42.  
  43.   LOCAL I%, J%, Count%, ParamPos%(), SearchChar$
  44.  
  45.   %L = 0    ' Named constants used to reference ParamPos%
  46.   %R = 1
  47.   DIM ParamPos%(0:%MAXPARAMETERS, %L:%R) ' Make room for position
  48.                        ' information
  49.  
  50.   IF Initialized% <> %TRUE THEN    ' We haven't parsed the command
  51.                   ' line yet
  52.     ' Set flag indicating we've parsed the command line
  53.     Initialized% = %TRUE    
  54.     IF COMMAND$ = "" THEN ' No command line parameters specified
  55.       FNParamCount% = 0      ' Return 0 for parameter count
  56.       Result% = 0      ' Save parameter count in static variable
  57.       EXIT DEF          ' Leave the function
  58.     ELSE    ' At least one command line parameter was specified
  59.       ' First we need to determine the number of parameters
  60.       I% = 1
  61.       WHILE (I% <= LEN(COMMAND$)) AND (Count% < %MAXPARAMETERS)
  62.     Count% = Count% + 1       ' Increment parameter counter
  63.     ParamPos%(Count%, %L) = I% ' Store left position of parameter
  64.     ' Determine what to search for as the end of the current
  65.     ' parameter
  66.     IF MID$(COMMAND$,I%,1) = CHR$(34) THEN
  67.     ' Parameter is enclosed in double quotes
  68.       SearchChar$ = CHR$(34)
  69.       ParamPos%(Count%, %L) = _            ' we don't want the "
  70.             ParamPos%(Count%, %L) + 1
  71.     ELSE
  72.       SearchChar$ = " "    ' look for a space
  73.     END IF
  74.     ' Check if the next character in the command line terminates
  75.     ' the current parameter
  76.     IF INSTR(I%+1,COMMAND$,SearchChar$) <> 0 THEN
  77.       ' find end of parameter
  78.       I% = INSTR(I%+1,COMMAND$,SearchChar$)
  79.       ' Store right position of parameter
  80.       ParamPos%(Count%, %R) = I%
  81.       ' Advance past the "
  82.       IF SearchChar$ = CHR$(34) THEN I% = I% + 1
  83.     ELSE
  84.       ' Store right position of parameter
  85.       ParamPos%(Count%, %R) = LEN(COMMAND$)    + 1
  86.       EXIT LOOP                
  87.     END IF
  88.     WHILE MID$(COMMAND$,I%,1) = " " AND I% < LEN(COMMAND$)
  89.       I% = I% + 1    ' now find the start of the next parameter
  90.     WEND
  91.       WEND
  92.  
  93.       ' next we need to store the parameters in our SHARED string
  94.       ' array
  95.  
  96.       FOR J% = 1 TO Count%     ' Store each of the parameters
  97.       Parameters$(J%) = MID$(COMMAND$, ParamPos%(J%, %L), _
  98.                   ParamPos%(J%, %R) - ParamPos%(J%, %L))
  99.       NEXT J%
  100.       FNParamCount% = Count%
  101.       Result% = Count%
  102.     END IF    ' COMMAND$ = ""
  103.   ELSE    ' The function has already been called once
  104.     FNParamCount% = Result%
  105.   END IF
  106. END DEF    ' FNParamCount%
  107.  
  108.  
  109. DEF FNParamStr$(Count%)
  110. ' Return the command line parameter indexed by Count%. The function
  111. ' verifies that the parameter exists by calling FNParamCount%(). If
  112. ' the parameter exists it is read from the global SHARED array
  113. ' Parameter$() and returned.
  114.  
  115.   SHARED Parameters$()    ' Global variable to store parameter data
  116.   LOCAL ParmCount%
  117.  
  118.   IF Count% <= FNParamCount% THEN    ' Check to make sure parameter
  119.     FNParamStr$ = Parameters$(Count%)    ' exists
  120.   ELSE
  121.     FNParamStr$ = ""
  122.   END IF
  123. END DEF    ' FNParamStr$()
  124.  
  125.  
  126.